/*
	awi_util containts Small tools.
	1. memory allocation and release
	2. debug print
	3. size end detection
	.....
*/
#ifndef _SRC_CORE_AWI_UTIL_H_
#define _SRC_CORE_AWI_UTIL_H_

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef USE_FREERTOS_HEAP
#include "FreeRTOS.h"
extern void* pvPortRealloc(void *ptr, size_t n);
extern void* pvPortCalloc(size_t m, size_t n);
#define awi_malloc pvPortMalloc
#define awi_calloc pvPortCalloc
#define awi_realloc pvPortRealloc
#define awi_free vPortFree
#else
#define awi_malloc malloc
#define awi_calloc calloc
#define awi_realloc realloc
#define awi_free free
#endif

#ifndef min
#define min(a,b) a<=b?a:b
#endif

#ifndef max
#define max(a,b) a>=b?a:b
#endif

#define CHECK(test)  \
  do {   \
    if (!(test)) {   \
      fprintf(stderr, "CHECK (%s: %s(): %d) %s \n", __FILE__, __func__,  \
              __LINE__, #test);  \
      abort();    \
    }   \
  } while (0)


#define aw_print(format,...) printf("[%s:%d] " format "\r\n", (char *)__FUNCTION__, __LINE__, ##__VA_ARGS__ )

#define awi_align(d,a) (((d) + (a - 1)) & ~(a - 1))

uint8_t awi_check_le(void);
uint8_t awi_check_be(void);

uint8_t awi_atoi(const char *data, int bytes, int *num);
uint8_t awi_strncmp(const char *d1, int l1, const char* d2, int l2);
	
extern uint32_t awi_htonl(uint32_t h);
extern uint32_t awi_ntohl(uint32_t n);
extern uint16_t awi_htons(uint16_t h);
extern uint16_t awi_ntohs(uint16_t n);
	
#ifdef __cplusplus
};
#endif
#endif
